import numpy as np
import neuron as nr


# 权重，初始化赋随机值
w1 = np.random.normal()
w2 = np.random.normal()
w3 = np.random.normal()
w4 = np.random.normal()
w5 = np.random.normal()
w6 = np.random.normal()

# 偏差，初始化赋随机值
b1 = np.random.normal()
b2 = np.random.normal()
b3 = np.random.normal()


# 定义神经节点
neuron_hiddenlayer_1 = nr.Neuron([w1, w2], b1)
neuron_hiddenlayer_2 = nr.Neuron([w3, w4], b2)
neuron_outputlayer = nr.Neuron([w5, w6], b3)


# 神经网络前馈计算
def feedforward_net(FP_inputs: list):
    """【函数】神经网络的前馈计算"""
    h1 = neuron_hiddenlayer_1.feedforward(FP_inputs)
    h2 = neuron_hiddenlayer_2.feedforward(FP_inputs)
    output_net = neuron_outputlayer.feedforward([h1, h2])
    return output_net


# 数据读取
data = np.loadtxt("data_train.txt", delimiter="\t", usecols=(1, 2, 3))
n = data.shape[0]   # 样本总数

x_input = data[:, 0:2]
y_true = data[:, 2]
print(data)
print(x_input)
print(y_true)

for (x, y) in zip(x_input, y_true):
    h1 = neuron_hiddenlayer_1.feedforward(x)
    h2 = neuron_hiddenlayer_2.feedforward(x)
    y_pred = neuron_outputlayer.feedforward([h1, h2])
    print("x = ", x)
    print("计算值:", y_pred)
    print("实际值:", y)

    # 偏导计算
    partial_L_partial_y_prep = -2*(y_true-y_pred)
    partial_y_pred_partial_h1 = w5*nr.deriv_sigmoid(h1*w5+h2*w6+b3)
    partial_h1_partial_w1 = x[0]*nr.deriv_sigmoid(x[0]*w1+x[1]*w2+b1)
